package com.nl.lib_core.tool.extends

import android.content.Context
import android.graphics.Point
import android.os.Build
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import androidx.annotation.FloatRange
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions

/** 作者: wangfei 创建时间: 2021/9/1 包名: com.nl.lib_core.util.extends 类名: ImageViewExtends */
/** 加载图片 */
fun ImageView.loadImage(
    context: Context,
    path: String,
    placeholder: Int? = null,
    useCache: Boolean = false
) {
  val options = getOptions(placeholder, useCache)
  Glide.with(context).load(path).apply(options).into(this)
}

fun ImageView.loadImage(
    context: Context,
    rawResId: Int,
    placeholder: Int? = null,
    useCache: Boolean = false
) {
  val options = getOptions(placeholder, useCache)
  Glide.with(context).load(rawResId).apply(options).into(this)
}

/** 加载圆形图片 */
fun ImageView.loadCircleImage(
    context: Context,
    path: String,
    placeholder: Int? = null,
    useCache: Boolean = false
) {
  val options = getOptions(placeholder, useCache)
  options.circleCrop()
  Glide.with(context).load(path).apply(options).into(this)
}

/** 加载圆角图片 */
fun ImageView.loadRoundCornerImage(
    context: Context,
    resourceId: Int,
    roundingRadius: Int = 20,
    placeholder: Int? = null,
    useCache: Boolean = false
) {
  val options = getOptions(placeholder, useCache)
  Glide.with(context)
      .load(resourceId)
      .apply(RequestOptions.bitmapTransform(RoundedCorners(roundingRadius)))
      .apply(options)
      .into(this)
}

/** 取消加载 */
fun ImageView.loadClear(context: Context) {
  Glide.with(context).clear(this)
}

/**
 * 按照图片的宽高比加载 使用本属性的ImageView在xml布局中的width及height属性必须为WRAP_CONTENT widthProportion
 * 为相对于屏幕宽度的比例取值范围为0.0-1.0，当widthProportion=1.0时，ImageView的宽度为屏幕宽度 heightProportion为相对于图片宽度的显示比例
 * @IntRange(from = 0, to = 1)
 */
fun ImageView.loadImageByProportion(
    @FloatRange(from = 0.0, to = 1.0) widthProportion: Float,
    heightProportion: Float
) {
  this.adjustViewBounds = true
  val wm: WindowManager? = this.context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
  val screenWidth =
      wm?.let {
        val point = Point()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
          wm.defaultDisplay?.getRealSize(point)
        } else {
          wm.defaultDisplay?.getSize(point)
        }
        point.x - leftMargin - rightMargin
      }
          ?: this.context.resources.displayMetrics.widthPixels - leftMargin - rightMargin
  val para = this.layoutParams
  para.width = (screenWidth * widthProportion).toInt()
  para.height = ViewGroup.LayoutParams.WRAP_CONTENT
  layoutParams = para
  maxWidth = (screenWidth * widthProportion).toInt()
  maxHeight = (screenWidth * widthProportion * heightProportion).toInt()
  scaleType = ImageView.ScaleType.CENTER_CROP
}

private fun ImageView.getOptions(
    placeholder: Int? = null,
    useCache: Boolean = false
): RequestOptions {
  val options = RequestOptions()
  placeholder?.let { options.placeholder(placeholder) }
  options.priority(Priority.HIGH)
  if (useCache) options.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
  return options
}
